require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')

require('../libs/fonts/iconfont.css')


require('../css/logon.less')

//引入验证码插件
const CaptchaMini = require("captcha-mini")

//引入axios插件
const axios = require("axios")



// 文档加载事件
document.addEventListener('DOMContentLoaded',function(){


  // 获取元素
  let phone = document.querySelector("#phone")//手机号
  let code = document.querySelector("#code")//验证码
  let pwd1 = document.querySelector("#pwd1")
  let pwd2 = document.querySelector("#pwd2")
  let logonBtn = document.querySelector("#logonBtn")//注册按钮
  let checking = document.querySelector("#checking")//验证





  /**
   * 1.验证码
  */

  // 定义变量接收
  let capt = '';

  let captcha = new CaptchaMini({
      lineWidth: 1,   //线条宽度
      lineNum: 4,       //线条数量
      dotR: 2,          //点的半径
      dotNum: 20,       //点的数量
      preGroundColor: [50, 80],    //前景色区间
      backGroundColor: [150, 250], //背景色区间
      fontSize: 55,           //字体大小
      fontFamily: ['Georgia', '微软雅黑', 'Helvetica', 'Arial'],  //字体类型
      fontStyle: 'fill',      //字体绘制方法，有fill和stroke
      content: '1234567890',  //验证码内容
      length: 4    //验证码长度
  }); 
  captcha.draw(document.querySelector('#captcha'), r => {
      // console.log(r, '验证码2');
      // console.log(r);
      capt = r;
  });






 /**
  * 2.需求：点击 logonBtn，验证input框
  */

  logonBtn.addEventListener('click',function(){

    let phoneVal = phone.value
    let codeVal = code.value
    let pwdVal1 = pwd1.value
    let pwdVal2 = pwd2.value

    // 判断
    if(!/^1[3-9][0-9]{9}$/.test(phoneVal)){
        // 手机号
        checking.textContent = '手机号错误';
        return;

    }else if(capt != code.value){
        // 验证码
        checking.textContent = '手机号错误';
        return;

    }else if(!/^[\w]{6,12}$/.test(pwdVal1)){//6-12：以字母开头，后跟数字 字母 下划线!/^[a-zA-Z]\w{5,11}$/.test(pwdVal1)
        //密码
        checking.textContent = '密码错误';
        return;

    }else if(pwdVal2 != pwdVal1){
        //在密码
        checking.textContent = '两次密码不一致';
        return;

    }else{
        // 都输入正确//清空错误信息
        checking.textContent = '';

        //发送请求-注册账号
        axios.post('http://139.9.177.51:8099/users/add', {
          account: phoneVal,
          password: pwdVal1
        }).then(function (r) {
          if (r.data.status === 0) {
            //成功-先弹一个轻提示
            //再跳转页面-跳登录
            
            location.href = './register.html'

          } else {
            //失败-先弹一个轻提示
            checking.textContent = '注册失败';
          };
        })

    }

  })




})


